package com.topway.dsmflex.ui.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.AppContext;
import com.swtdesigner.SWTResourceManager;

/**
 * 
 * @author liusy 2013-05-10
 *
 */
public class SysConfig extends Dialog {
   public Text txtFilePath;
   public Text txtIndexPath;
   protected Object result;
   protected Shell  shell;

   
   /**
    * Create the dialog
    * @param parent
    * @param style
    */
   public SysConfig(Shell parent, int style) {
      super(parent, style);
   }

   /**
    * Create the dialog
    * @param parent
    */
   public SysConfig(Shell parent) {
      this(parent, SWT.NONE);
   }

   /**
    * Open the dialog
    * @return the result
    */
   public Object open() {
      createContents();
      Display display = getParent().getDisplay();
      setPos(display);
      shell.open();
      shell.layout();
      
      txtFilePath.setText(AppContext.FileRootPath);
      txtIndexPath.setText(AppContext.IndexPath);
      
      while (!shell.isDisposed()) {
         if (!display.readAndDispatch()) display.sleep();
      }
      return result;
   }

   /**
    * Create contents of the dialog
    */
   protected void createContents() {
      shell = new Shell(getParent(), SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
      shell.setLayout(new FormLayout());
      shell.setSize(600, 350);
      shell.setText("系统配置");
      
      final Button btnSave = new Button(shell, SWT.NONE);
      btnSave.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(final SelectionEvent e) {
        	 String filepath=System.getProperty("user.dir") + "/config.properties";
             AppContext.writeProperties(filepath, "path.fileRoot",txtFilePath.getText() );
             AppContext.writeProperties(filepath, "path.fullIndex",txtIndexPath.getText() );
             
            // AppContext.init();
            shell.dispose();
         }
      });
      final FormData fd_btnSave = new FormData();
      fd_btnSave.width=70;
      fd_btnSave.left = new FormAttachment(50, -90);
      fd_btnSave.bottom = new FormAttachment(100, -5);
      btnSave.setLayoutData(fd_btnSave);
      btnSave.setText("保存(&S)");

      final Button btnCancel = new Button(shell, SWT.NONE);
      btnCancel.addSelectionListener(new SelectionAdapter() {
         public void widgetSelected(final SelectionEvent e) {
            shell.dispose();
         }
      });
      final FormData fd_btnCancel = new FormData();
      fd_btnCancel.width=70;
      fd_btnCancel.left = new FormAttachment(50, 20);
      fd_btnCancel.bottom = new FormAttachment(100, -5);
      btnCancel.setLayoutData(fd_btnCancel);
      btnCancel.setText("取消(&C)");

      final Composite composite = new Composite(shell, SWT.NONE);
      composite.setLayout(new FormLayout());
      final FormData fd_composite = new FormData();
      fd_composite.bottom = new FormAttachment(100, -35);
      fd_composite.top = new FormAttachment(0, 0);
      fd_composite.left = new FormAttachment(0, 1);
      fd_composite.right = new FormAttachment(100, -1);
      composite.setLayoutData(fd_composite);

      final Group grpAsssss = new Group(composite, SWT.NONE);
      grpAsssss.setText("同步配置");
      grpAsssss.setLayout(new FormLayout());
      final FormData fd_grpAsssss = new FormData();
      fd_grpAsssss.bottom = new FormAttachment(0, 176);
      fd_grpAsssss.top = new FormAttachment(0, 5);
      grpAsssss.setLayoutData(fd_grpAsssss);


      Group group_1;
      group_1 = new Group(composite, SWT.NONE);
      fd_grpAsssss.right = new FormAttachment(group_1, 590, SWT.LEFT);
      fd_grpAsssss.left = new FormAttachment(group_1, 0, SWT.LEFT);

     
      final FormData fd_group_1 = new FormData();
      fd_group_1.bottom = new FormAttachment(100, -1);
      fd_group_1.top = new FormAttachment(grpAsssss, 0);
      fd_group_1.right = new FormAttachment(100, -1);
      fd_group_1.left = new FormAttachment(0, 1);
      group_1.setLayoutData(fd_group_1);
      group_1.setLayout(new FormLayout());
      group_1.setText(" 全文索引 ");

      final Label label = new Label(group_1, SWT.NONE);
      label.setAlignment(SWT.RIGHT);
      final FormData fd_label = new FormData();
      fd_label.top = new FormAttachment(0, 5);
      fd_label.left = new FormAttachment(0, 5);
      fd_label.width=100;
      label.setLayoutData(fd_label);
      label.setText("索引文件目录：");

      final Label label_1 = new Label(group_1, SWT.NONE);
      label_1.setAlignment(SWT.RIGHT);
      final FormData fd_label_1 = new FormData();
      fd_label_1.top = new FormAttachment(label, 7);
      fd_label_1.left = new FormAttachment(0, 5);
      fd_label_1.width=100;
      label_1.setLayoutData(fd_label_1);
      label_1.setText("附件存放目录：");

      txtIndexPath = new Text(group_1, SWT.BORDER);
      txtIndexPath.setBackground(SWTResourceManager.getColor(255, 255, 255));
      txtIndexPath.setEditable(false);
      final FormData fd_txtIndexPath = new FormData();
      fd_txtIndexPath.right = new FormAttachment(100, -23);
      fd_txtIndexPath.left = new FormAttachment(label, 1);
      fd_txtIndexPath.top = new FormAttachment(0, 2);
      txtIndexPath.setLayoutData(fd_txtIndexPath);

      txtFilePath = new Text(group_1, SWT.BORDER);
      txtFilePath.setBackground(SWTResourceManager.getColor(255, 255, 255));
      txtFilePath.setEditable(false);
      final FormData fd_txtFilePath = new FormData();
      fd_txtFilePath.top = new FormAttachment(txtIndexPath, 2);
      fd_txtFilePath.left = new FormAttachment(label_1, 1);
      fd_txtFilePath.right = new FormAttachment(100, -23);
      txtFilePath.setLayoutData(fd_txtFilePath);

      final Button btnIndexPath = new Button(group_1, SWT.NONE);
      btnIndexPath.addSelectionListener(new SelectionAdapter() {
      	public void widgetSelected(final SelectionEvent e) {
	  	  Button btn = (Button) e.getSource();
	  	  DirectoryDialog dialog = new DirectoryDialog(btn.getShell(), SWT.OPEN);
	  	  dialog.setFilterPath(AppContext.IndexPath);
          String path = dialog.open();
          if (path != null) {
        	  txtIndexPath.setText(path);
          }
      	}
      });
      final FormData fd_btnIndexPath = new FormData();
      fd_btnIndexPath.top = new FormAttachment(txtIndexPath, 0,SWT.TOP);
      fd_btnIndexPath.right = new FormAttachment(100, -1);
      fd_btnIndexPath.height=22;
      btnIndexPath.setLayoutData(fd_btnIndexPath);
      btnIndexPath.setText("...");

      final Button btnFilePath = new Button(group_1, SWT.NONE);
      btnFilePath.addSelectionListener(new SelectionAdapter() {
      	public void widgetSelected(final SelectionEvent e) {
      		Button btn = (Button) e.getSource();
  	  	    DirectoryDialog dialog = new DirectoryDialog(btn.getShell(), SWT.OPEN);
  	  	    dialog.setFilterPath(AppContext.FileRootPath);
            String path = dialog.open();
            if (path != null) {
            	txtFilePath.setText(path);
            }
      	}
      });
      final FormData fd_btnFilePath = new FormData();
      fd_btnFilePath.top = new FormAttachment(txtFilePath, 0,SWT.TOP);
      fd_btnFilePath.right = new FormAttachment(100, -1);
      fd_btnFilePath.height=22;
      btnFilePath.setLayoutData(fd_btnFilePath);
      btnFilePath.setText("...");
   }


   private void setPos(Display display) {
      Rectangle rec = Display.getCurrent().getClientArea();
      shell.setLocation((rec.width - shell.getSize().x)/2, (rec.height - shell.getSize().y)/2);
   }

}
